package com.glory.wxelectricitypda.activity;


import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.FTDataAcqAdapter;
import com.glory.wxelectricitypda.adapter.FTReportMESAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.FTDataAcqQueryBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class FTReportMESActivity extends BaseDialogActivity {


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.testProgressRecycler)
    RecyclerView testProgressRecycler;
    @BindView(R.id.lotID)
    FreeUI_ClearEditText lotID;
    @BindView(R.id.eqpID)
    FreeUI_ClearEditText eqpID;
    @BindView(R.id.testeqpId)
    FreeUI_ClearEditText testeqpId;
    @BindView(R.id.lotQty)
    TextView lotQty;
    @BindView(R.id.binRecycler)
    RecyclerView binRecycler;
    @BindView(R.id.beginAcq)
    TextView beginAcq;


    private FTDataAcqAdapter mFTDataAcqAdapter;
    private FTReportMESAdapter mFTReportMESAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ftreport_mesactivity;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("FT数据采集");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (testeqpId.getText().toString().isEmpty()){
                    showToast("请扫描测试机id",false);
                    return;
                }

                submit();
            }
        });

        testProgressRecycler.setLayoutManager(new LinearLayoutManager(FTReportMESActivity.this));
        testProgressRecycler.setItemAnimator(new DefaultItemAnimator());
        testProgressRecycler.addItemDecoration(new DividerItemDecoration(FTReportMESActivity.this, 1));
        mFTDataAcqAdapter = new FTDataAcqAdapter(null);
        mFTDataAcqAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mFTDataAcqAdapter.isFirstOnly(true);
        testProgressRecycler.setAdapter(mFTDataAcqAdapter);

        binRecycler.setLayoutManager(new LinearLayoutManager(FTReportMESActivity.this));
        binRecycler.setItemAnimator(new DefaultItemAnimator());
        binRecycler.addItemDecoration(new DividerItemDecoration(FTReportMESActivity.this, 1));
        mFTReportMESAdapter = new FTReportMESAdapter(null);
        mFTReportMESAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mFTReportMESAdapter.isFirstOnly(true);
        binRecycler.setAdapter(mFTReportMESAdapter);

    }

    @Override
    protected void initData() {

        initEdit(testeqpId,s ->{
            if (lotID.getText().toString().isEmpty()){
                showToast("请扫描批次id",false);
                return;
            }
            if (eqpID.getText().toString().isEmpty()){
                showToast("请扫描主设备id",false);
                return;
            }

            queryDataMessage(true);
        });

        beginAcq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (testeqpId.getText().toString().isEmpty()){
                    showToast("请扫描测试机id",false);
                    return;
                }

                queryDataMessage(false);
            }
        });

    }

    private void queryDataMessage(boolean type){


        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID.getText().toString());
            jsonObject.put("TesteqpId", testeqpId.getText().toString());
            jsonObject.put("LotID", lotID.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("getHbinQty","getHbinQty",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<FTDataAcqQueryBean>>(
                new TypeToken<ResponseBean<FTDataAcqQueryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<FTDataAcqQueryBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    if (type){
                        lotQty.setText(response.body().getBody().getLotQty());
                        mFTDataAcqAdapter.setNewData(response.body().getBody().getTestProg());
                        mFTReportMESAdapter.setNewData(response.body().getBody().getHbinEDClist());
                    }else {
                        mFTReportMESAdapter.setNewData(response.body().getBody().getHbinEDClist());
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<FTDataAcqQueryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "getHbinQty");
            }
        });

    }

    private void submit(){

        if (mFTDataAcqAdapter.getTestRecId() == -1){
            showToast("请选择测试进度",false);
            return;
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID.getText().toString());
            jsonObject.put("TesteqpId", testeqpId.getText().toString());
            jsonObject.put("LotID", lotID.getText().toString());
            jsonObject.put("TestRecId", mFTDataAcqAdapter.getData().get(mFTDataAcqAdapter.getTestRecId()).getPdaMasterID());
            JSONArray jsonArray = new JSONArray(new Gson().toJson(mFTReportMESAdapter.getData()));
            jsonObject.put("HbinEDClist", jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("escaLation","escaLation",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<Object>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功",true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    },2000);
                }
            }

            @Override
            public void onError(Response<ResponseBean<Object>> response) {
                super.onError(response);
                loadError(response.getException(), "escaLation");
            }
        });


    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }



}